package com.hui.account.utils;

import android.content.Context;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hui.account.R;

/**
 * 全局提示框
 */
public class ToastUtils {

    // 解决 Toast重复显示问题
    private static Toast toast = null;

    /**
     * 结果：成功
     */
    public static void success(Context context, String message, int gravity, int x, int y) {
        if(null != toast)
            toast.cancel();

        toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        toast.setGravity(gravity, x, y);

        LinearLayout view = (LinearLayout) toast.getView();

        // 修改字体颜色
        TextView tv = (TextView) view.getChildAt(0);
        tv.setTextColor(context.getResources().getColor(R.color.cyan_4CAF50));

        // 修改背景颜色
        view.setBackgroundResource(R.drawable.toast_success_background);


        toast.show();
    }

    /**
     * 结果：失败
     */
    public static void error(Context context, String message, int gravity, int x, int y) {
        if(null != toast)
            toast.cancel();

        toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        toast.setGravity(gravity, x, y);

        LinearLayout view = (LinearLayout) toast.getView();

        // 修改字体颜色
        TextView tv = (TextView) view.getChildAt(0);
        tv.setTextColor(context.getResources().getColor(R.color.red_FF5A5A));

        // 修改背景颜色
        view.setBackgroundResource(R.drawable.toast_error_background);


        toast.show();
    }
}
